package com.jhh.pay.haier.service.impl;

import com.jhh.pay.channel.pojo.RequestBean;
import com.jhh.pay.channel.pojo.ResponseBean;
import com.jhh.pay.channel.service.PayService;
import com.jhh.pay.haier.bean.HaierPaymentRequest;
import com.jhh.pay.haier.bean.HaierQueryTradeRequest;
import com.jhh.pay.haier.bean.HaierWithholdRequest;
import com.jhh.pay.haier.util.haierUtils.GetBank;
import com.jhh.pay.haier.util.haierUtils.HaierConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 2017/12/8.
 */
@Service
public class HaierPaymentServiceImpl extends HaierBaseServiceImpl implements PayService{

    private static final Logger logger = LoggerFactory.getLogger(HaierPaymentServiceImpl.class);
    @Override
    public String getChannelName() {
        return "HaierPay";
    }

    @Override
    public ResponseBean pay(RequestBean request) {
        //组装参数
        HaierPaymentRequest payment = new HaierPaymentRequest();
        payment.setOuter_trade_no(request.getOrderNo());
        payment.setPayable_amount(request.getMoney());
        String bankNo = request.getBankInfo().getBankNo();
        //TODO: 维护银行及信息
        String linkCode = GetBank.getname(bankNo);
        payment.setBank_code(linkCode);
        String bankName = GetBank.getname(bankNo);
        payment.setBank_name(bankName);
        String haierKey = request.getExtension().get("haierKey");
        //银行卡号加密
        payment.setCard_no(kjtEncryptService.encrypt(request.getBankInfo().getBankCard(),haierKey));
        //证件号加密
        payment.setAccount_name( kjtEncryptService.encrypt(request.getBankInfo().getPersonalName(),haierKey));
        payment.setService(HaierConstants.SERVICE_PAYMENT);
        payment.setVersion(HaierConstants.VERSION);
        payment.setPartner_id(request.getExtension().get("partner_id"));
        payment.set_input_charset(HaierConstants._INPUT_CHARSET);
        payment.setSign_type(HaierConstants.SIGN_TYPE);
        payment.setIdentity_no(request.getExtension().get("identity_no"));
        payment.setIdentity_type(HaierConstants.IDENTITY_TYPE);
        payment.setCompany_or_personal(HaierConstants.COMPANY_OR_PERSONAL);
        payment.setFundout_grade(HaierConstants.FUNDOUT_GRADE);
        //  payment.setNotify_url(notify_url_payment);
        logger.info("--------------------->>>>>>>>单笔付款到卡请求参数--------------<<<<<<<<<\n"+payment);
        //调用第三方接口
        return is_success(payment);

    }

    @Override
    public ResponseBean deduct(RequestBean request) {

        HaierWithholdRequest withhold = new HaierWithholdRequest();
        withhold.setOuter_trade_no(request.getOrderNo());
        withhold.setUser_name(request.getExtension().get("cardNo"));
        withhold.setCertificates_type("1");
        String bankNo = request.getBankInfo().getBankNo();
        String linkCode = GetBank.getname(bankNo);
        withhold.setBank_code(linkCode);
        withhold.setPayable_amount(request.getMoney());
        withhold.setPartner_id(request.getExtension().get("partner_id"));
        logger.info("--------------------->>>>>>>>代扣请求参数--------------<<<<<<<<<\n" + withhold);
        String haierKey = request.getExtension().get("haierKey");
        //银行卡号加密
        withhold.setCard_no(kjtEncryptService.encrypt(request.getBankInfo().getBankCard(), haierKey));
        //证件号加密
        withhold.setCertificates_number(kjtEncryptService.encrypt(request.getExtension().get("certificates_number"), haierKey));
        withhold.setService(HaierConstants.SERVICE_WITHHOLD);
        withhold.setVersion(HaierConstants.VERSION);
        withhold.setPartner_id(request.getExtension().get("partner_id"));
        withhold.set_input_charset(HaierConstants._INPUT_CHARSET);
        withhold.setSign_type(HaierConstants.SIGN_TYPE);
        return is_success(withhold);

    }

    @Override
    public ResponseBean callbackResolve(RequestBean request) {
        return null;
    }

    @Override
    public ResponseBean state(RequestBean request) {
        //组装请求参数
        HaierQueryTradeRequest trade = new HaierQueryTradeRequest();
        trade.setOuter_trade_no(request.getOrderNo());
        trade.setPartner_id(request.getExtension().get("partner_id"));
        assembly(trade,"withhold");
        //------------------------真实请求第三方查询---------------
        Map<String, String> map = new HashMap<>();
        try {
            map = post(trade, "withhold");
        }catch (Exception e){
            logger.error("海尔金融第三方出现异常 -------",e);
            return ResponseBean.FAIL_RESPONSE();
        }
        //判断订单是否查询成功
        return trade_success(map);
    }

    @Override
    public ResponseBean structure(RequestBean request) {
        return null;
    }

    /**
     *  查询接口组装基本参数
     * @param trade
     * @param type
     */
    private void assembly(HaierQueryTradeRequest trade, String type){
        if ("withhold".equals(type)){
            trade.setService(HaierConstants.QUERY_TRADE);
        }else {
            trade.setService(HaierConstants.QUERY_FUND_OUT);
        }
        trade.setVersion(HaierConstants.VERSION);
        trade.set_input_charset(HaierConstants._INPUT_CHARSET);
        trade.setSign_type(HaierConstants.SIGN_TYPE);
    }

}
